import Vue from 'vue'
import Vuex from 'vuex'
import vuexPersist  from "vuex-persist";
Vue.use(Vuex)
export default new Vuex.Store({
  state: {
    todolist: [
      {
        title: "孙悟空",
        state: 0,
        //0代表未完成
        time: "2020-8-14",
      },
      {
        title: "猪八戒",
        state: 1,
        //1代表已完成
        time: "2020-8-14",
      },
      {
        title: "沙和尚",
        state: 2,
        //代表取消
        time: "2020-8-14",
      },
    ],
  },
  mutations: {
    add(state,obj){
      state.todolist.push(obj);
    },
    change(state, i) {
      state.todolist.forEach((item, index) => {
        if (index == i) {
          if (item.state == 0) {
            item.state = 1;
            item.time = new Date().toLocaleString();
          } else {
            item.state = 0;
          }
        }
      });
    },
    //点击未完成变成已完成 点击已完成变成未完成
    cancel(state, i) {
      state.todolist.forEach((item, index) => {
        if (index == i) {
          if (item.state == 0) {
            item.state = 2;
          } else {
            item.state = 0;
          }
        }
      });
    },
    // 删除
    del(state,index){
          state.todolist.splice(index,1);
          
    }
  },
  actions: {
  },
  modules: {
  },
  plugins: [
    new vuexPersist({
      storage: window.localStorage,
    }).plugin,
  ],
})